package com.efast.cafe.portal.service.base.impl;

import java.rmi.ServerException;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import com.efast.cafe.framework.constant.CommonConstant;
import com.efast.cafe.framework.exception.ServiceException;
import com.efast.cafe.framework.jdbc.JdbcDao;
import com.efast.cafe.framework.jdbc.JdbcDaoImpl;
import com.efast.cafe.framework.jdbc.persistence.Criteria;
import com.efast.cafe.framework.pager.Pagination;
import com.efast.cafe.framework.redis.ObjectRedisTemplate;
import com.efast.cafe.framework.service.CafeServiceImpl;
import com.efast.cafe.framework.util.SpringWebUtils;
import com.efast.cafe.portal.bean.base.CompanyBaseLovBean;
import com.efast.cafe.portal.bean.base.CompanyRecoveryDefaultLovBean;
import com.efast.cafe.portal.bean.base.DelCompanyLovBean;
import com.efast.cafe.portal.bean.base.PortalBaseLovBean;
import com.efast.cafe.portal.bean.base.PortalBaseLovQueryBean;
import com.efast.cafe.portal.dao.base.PortalBaseLovDao;
import com.efast.cafe.portal.entity.base.PortalBaseLov;
import com.efast.cafe.portal.entity.base.PortalBaseParameter;
import com.efast.cafe.portal.remote.base.PortalBaseLovService;
import com.efast.cafe.portal.service.base.IPortalBaseLovService;
import com.efast.cafe.portal.service.common.IParameterService;
import com.github.pagehelper.PageHelper;

/******************************************************************************
 * @File name : BaseLovServiceImpl.java
 * 
 * @Author : xl
 * 
 * @Date : 2016-6-7
 * 
 * @Copyright Notice: Copyright (c) 2016 Efast Serv. All Rights Reserved. This
 *            software is published under the terms of the Efast Software
 *            License version 1.0, a copy of which has been included with this
 *            distribution in the LICENSE.txt file.
 * 
 * 
 *            ------------------------------------------------------------------
 *            ---------- Date Who Version Comments 2016-6-7 上午10:51:55 xl 1.0
 *            Initial Version
 *****************************************************************************/
@Service
@RestController
public class PortalBaseLovServiceImpl extends CafeServiceImpl<PortalBaseLov> implements IPortalBaseLovService {

	/*
	 * private IPortalBaseLovDao portalBaseLovDao ;
	 * 
	 * 
	 * private IPortalBaseParameterDao portalBaseParameterDao;
	 */

	private PortalBaseLovDao portalBaseLovDao;
	
	@Autowired
	private IParameterService parameterService;
	
	private ObjectRedisTemplate objectRedisTemplate;
	
	@Autowired
	private StringRedisTemplate stringRedisTemplate;

	
	@Qualifier("jdbcDao")
	@Autowired
	private JdbcDao jdbcDao;

	/*
	 * @Autowired public void setPortalBaseLovDao(IPortalBaseLovDao
	 * portalBaseLovDao) { this.portalBaseLovDao=portalBaseLovDao; }
	 * 
	 * @Autowired public void setPortalBaseParameterDao(IPortalBaseParameterDao
	 * portalBaseParameterDao) {
	 * this.portalBaseParameterDao=portalBaseParameterDao; }
	 */
	@Autowired
	public void setPortalBaseLovDao(PortalBaseLovDao portalBaseLovDao) {
		this.portalBaseLovDao = portalBaseLovDao;
	}

	

	
	@Autowired
	public void setObjectRedisTemplate(ObjectRedisTemplate objectRedisTemplate) {
		this.objectRedisTemplate = objectRedisTemplate;
	}

	/**
	 * {@inheritDoc} overridden:
	 * 
	 * @throws ServerException
	 * 
	 * @Date : 2016-6-7
	 * @see org.eae.eldf.service.base.BaseLovService#updateBaseLov(org.eae.eldf.entity.base.BaseLov)
	 **/
	@Override
	public void updatePortalBaseLov(PortalBaseLov portalBaseLov) {
		Criteria criteria = Criteria.create(PortalBaseLov.class)
				.set(PortalBaseLov.Id, portalBaseLov.getId().substring(0, portalBaseLov.getId().lastIndexOf("."))+ "." + portalBaseLov.getOption_key())
				.set(PortalBaseLov.paramKey, portalBaseLov.getParam_key())
				.set(PortalBaseLov.moduleName, portalBaseLov.getModule_name())
				.set(PortalBaseLov.i18nParamKey, portalBaseLov.getId() + "." + portalBaseLov.getOption_desc())
				.set(PortalBaseLov.dataType, portalBaseLov.getData_type())
				.set(PortalBaseLov.optionKey, portalBaseLov.getOption_key())
				.set(PortalBaseLov.optionOrder, portalBaseLov.getOption_order())
				.set(PortalBaseLov.optionDesc, portalBaseLov.getOption_desc())
				.set(PortalBaseLov.i18nOptionDesc,
						portalBaseLov.getId() + "." + portalBaseLov.getParam_key() + "."
								+ portalBaseLov.getOption_desc())
				.set(PortalBaseLov.parentId, portalBaseLov.getParent_id())
				.set(PortalBaseLov.Valid, portalBaseLov.getValid())
				.set(PortalBaseLov.lastUpdateUser, SpringWebUtils.getRequestUser())
				.where(PortalBaseLov.portalBaseLovId, new Object[] { portalBaseLov.getPortal_base_lov_id() });

		jdbcDao.update(criteria);
		//stringRedisTemplate.delete(CommonConstant.SESSION_LOV_VERSION);
		stringRedisTemplate.opsForHash().put(CommonConstant.SESSION_LOV_VERSION, "BASIC", UUID.randomUUID().toString());
		
	}

	/**
	 * {@inheritDoc} overridden:
	 * 
	 * @Date : 2016-6-7
	 * @see org.eae.eldf.service.base.BaseLovService#saveBaseLov(org.eae.eldf.entity.base.BaseLov)
	 **/
	@Override
	public void savePortalBaseLov(PortalBaseLov portalBaseLov) {
		Criteria criteria = Criteria.create(PortalBaseLov.class)
				.set(PortalBaseLov.Id, portalBaseLov.getId() + "." + portalBaseLov.getOption_key())
				.set(PortalBaseLov.paramKey, portalBaseLov.getParam_key())
				.set(PortalBaseLov.moduleName, portalBaseLov.getModule_name())
				.set(PortalBaseLov.i18nParamKey, portalBaseLov.getId() + "." + portalBaseLov.getOption_desc())
				.set("dataType", portalBaseLov.getData_type())
				.set(PortalBaseLov.optionKey, portalBaseLov.getOption_key())
				.set(PortalBaseLov.optionOrder, portalBaseLov.getOption_order())
				.set(PortalBaseLov.optionDesc, portalBaseLov.getOption_desc())
				.set(PortalBaseLov.i18nOptionDesc,
						portalBaseLov.getId() + "." + portalBaseLov.getParam_key() + "."
								+ portalBaseLov.getOption_desc())
				.set(PortalBaseLov.parentId, portalBaseLov.getParent_id())
				.set(PortalBaseLov.Valid, portalBaseLov.getValid())
				.set(PortalBaseLov.lastUpdateUser, SpringWebUtils.getRequestUser())
				.set(PortalBaseLov.createUser, SpringWebUtils.getRequestUser());
	//	stringRedisTemplate.delete(CommonConstant.SESSION_LOV_VERSION);
		stringRedisTemplate.opsForHash().put(CommonConstant.SESSION_LOV_VERSION, "BASIC", UUID.randomUUID().toString());
		jdbcDao.insert(criteria);
	}

	@Override
	public void deletePortalBaseLov(Long portalBasLovId) {
		if (isLastNode(portalBasLovId)) {
			throw new ServiceException("含有子节点不能删除");
		}
		//stringRedisTemplate.delete(CommonConstant.SESSION_LOV_VERSION);
		stringRedisTemplate.opsForHash().put(CommonConstant.SESSION_LOV_VERSION, "BASIC", UUID.randomUUID().toString());
		jdbcDao.delete(PortalBaseLov.class, portalBasLovId);
	}

	/**
	 * {@inheritDoc} overridden:
	 * 
	 * @Date : 2016-6-7
	 * @see 通过ID查找 该节点下所属lov子节点
	 **/
	@Override
	public Pagination<PortalBaseLov> getPortalBaseLovListById(Pagination<PortalBaseLov> pager, Long protalBaseLovId,
			boolean flag) {
		/*
		 * return
		 * portalBaseLovDao.getBaseLovListById(pager,protalBaseLovId,flag);
		 */
		Criteria criteria = Criteria.create(PortalBaseLov.class).where(PortalBaseLov.Valid, new Object[] { 1 });
		if (null != protalBaseLovId) {
			criteria.and(PortalBaseLov.parentId, new Object[] { protalBaseLovId });
		}
		if (flag) {
			criteria.and(PortalBaseLov.enterpriseId, new Object[] { SpringWebUtils.getRequestCompany() });
		}
		return jdbcDao.queryListByPage(pager, criteria);
	}

	/**
	 * {@inheritDoc} overridden:
	 * 
	 * @Date : 2016-6-7
	 * @see org.eae.eldf.service.base.BaseLovService#getBaseLovList()
	 **/
	@Override
	public List<PortalBaseLov> getPortalBaseLovListTree(String paramKey) {
		
		List<PortalBaseLov> list = portalBaseLovDao.queryListByParamKey(paramKey);
		boolean islast = true;
		List<PortalBaseLov> node = list;
		while (islast) {
			if (node.size() > 0) {
				node = this.queryListNextNode(node);
				list.addAll(node);
			} else {
				islast = false;
			}
		}

		return list;
	}

	/**
	 * {@inheritDoc} overridden:
	 * 
	 * @Date : 2016-6-20
	 * @see org.eae.eldf.portal.remote.base.PortalBaseLovService#getPortalBaseLovById(java.lang.Long)
	 **/
	@Override
	public PortalBaseLov getPortalBaseLovById(Long portalBasLovId) {
		// TODO Auto-generated method stub
		return jdbcDao.get(PortalBaseLov.class, portalBasLovId);
	}

	/**
	 * 判断该lov参数是否存在下属参数
	 * 
	 * @Date : 2016-7-22
	 * @param portalBasLovId
	 * @return
	 */
	public boolean isLastNode(Long portalBasLovId) {
		PortalBaseLov portalBaseLov = getPortalBaseLovById(portalBasLovId);
		Criteria criteria = Criteria.create(PortalBaseLov.class)
				.where(PortalBaseLov.parentId, new Object[] { portalBasLovId })
				.and(PortalBaseLov.paramKey, new Object[] { portalBaseLov.getParam_key() });
		int result = jdbcDao.queryCount(criteria);
		if (result > 0) {
			return true;
		} else {
			return false;
		}
	}

	/**
	 * {@inheritDoc} overridden:
	 * 
	 * @Date : 2016-8-9
	 * @see org.eae.eldf.portal.remote.base.PortalBaseLovService#lovPage(java.lang.Long)
	 **/
	@Override
	public Map<String, Object> lovPage(Long portalBaseParameterId) {
		Map<String, Object> data = new HashMap<String, Object>();
		PortalBaseParameter portalBaseParameter = jdbcDao.get(PortalBaseParameter.class, portalBaseParameterId);
		// 保存当前参数类型信息
		data.put("portalBaseParameter", portalBaseParameter);

		Criteria criteria = Criteria.create(PortalBaseLov.class).where("param_key",
				new Object[] { portalBaseParameter.getParam_key() });
		List<PortalBaseLov> list = jdbcDao.queryList(criteria);
		// 保存当前参数类型 下所有lov参数
		data.put("listLov", list);

		//List<PortalBaseLov> listParams = this.queryByParamKey(portalBaseParameter.getParam_key());

		// 查询该节点上层节点
		/*boolean isFirst = false;
		List<PortalBaseLov> listLov = null;
		List<PortalBaseLov> listFirst = new ArrayList<PortalBaseLov>();
		while (!isFirst) {
			System.out.println("1111111111");
			listLov = this.queryByParentId(listParams);
			if (listLov != null && listLov.size() > 0) {
				listFirst.addAll(this.queryListParamKey(listLov));
				listParams = this.queryGroupByParentId(listLov);
				if (!(listLov.size() > 0)) {
					isFirst = true;
				}
			} else {
				isFirst = true;
			}
		}
		// 保存包含当前参数类型 的顶级类型
		data.put("listFirst", listFirst);*/
		Criteria parameter = Criteria.create(PortalBaseParameter.class).where("data_type", new Object[] { "LOV" });
		List<PortalBaseParameter> listParameter = jdbcDao.queryList(parameter);
		data.put("listParameter", listParameter);
		return data;
	}
	
	public List<PortalBaseLov> queryByParamKey(String paramKey) {

		return portalBaseLovDao.queryByParamKey(paramKey);
	}

	public List<PortalBaseLov> queryGroupByParentId(List<PortalBaseLov> list) {

		return portalBaseLovDao.queryGroupByParentId(list);
	}

	public List<PortalBaseLov> queryByParentId(List<PortalBaseLov> list) {

		if (list.size() > 0) {
			return portalBaseLovDao.queryByParentId(list);
		} else {
			return null;
		}
	}

	public List<PortalBaseLov> queryListParamKey(List<PortalBaseLov> list) {
		if (list.size() > 0) {
			return portalBaseLovDao.queryListParamKey(list);
		} else {
			return null;
		}
	}

	public List<PortalBaseLov> queryListNextNode(List<PortalBaseLov> list) {
		if (list.size() > 0) {
			return portalBaseLovDao.queryListNextNode(list);
		} else {
			return null;
		}

	}

	

	@Override
	public List<PortalBaseLov> getPortalBaseLovParamKeyTree(String paramKey, String moduleName) {

		List<PortalBaseLov> list = getPortalBaseLovSelect(paramKey, moduleName);
		boolean islast = true;
		List<PortalBaseLov> node = list;
		while (islast) {
			if (node.size() > 0) {
				node = this.queryListNextNode(node);
				list.addAll(node);
			} else {
				islast = false;
			}
		}

		return list;

	}
	
	@Override
	public List<PortalBaseLov> getPortalBaseLovListTrees(String paramKey) {
		return getPortalBaseLovListTree(paramKey);
	}

	@Override
	public List<PortalBaseLov> getPortalBaseLovSelect(String paramKey, String moduleName) {
	
		PortalBaseParameter param =	parameterService.getParameter(paramKey, moduleName);
		return	portalBaseLovDao.queryListByCompanyOrUser(paramKey, param.getId(), param.getUser_id(), param.getEnterprise_id());
	}

	@Override
	public Map<String, List<PortalBaseLov>> getPortalBaseLovSelectList(List<String> paramKeys, String moduleName) {
		Map<String, List<PortalBaseLov>> reMap=new HashMap<>();
		List<PortalBaseLov> result = new ArrayList<>();
		for (String paramKey : paramKeys) {
			result=getPortalBaseLovSelect(paramKey, moduleName);
			reMap.put(paramKey, result);
		}
		return reMap;
	}

	@SuppressWarnings("unchecked")
	@Override
	public Map<String, Object> getPortalBaseLovSelectListByModule(PortalBaseLovBean portalBaseLovBean) {
		List<PortalBaseLov> list=portalBaseLovBean.getListLov();
		
		List<PortalBaseLov> result = new ArrayList<>();
		Map<String, Object> map =new HashMap<>();
		for (PortalBaseLov portalBaseLov : list) {
			Map<String, List<PortalBaseLov>> reMap = null;
			String moduleName = portalBaseLov.getModule_name();
			if (map.containsKey(moduleName)) {
				reMap = (Map<String, List<PortalBaseLov>>) map.get(moduleName);
			} else {
				reMap =new HashMap<>();
			}
			result=getPortalBaseLovSelect(portalBaseLov.getParam_key(), moduleName);
			reMap.put(portalBaseLov.getParam_key(), result);
			map.put(portalBaseLov.getModule_name(),reMap);
		}
		
		return map;
	}

	@Override
	public PortalBaseLovQueryBean queryLovByCompanyParameterId(PortalBaseLovQueryBean queryBean) {
		String companyCode = StringUtils.isNotBlank(queryBean.getCompanyCode())?queryBean.getCompanyCode():SpringWebUtils.getRequestCompany();
		PortalBaseParameter param = parameterService.getParameter(queryBean.getParamKey(), queryBean.getModuleName(),companyCode,null);
		if(null!=param){
			String id = "";
			if(StringUtils.isNotBlank(param.getEnterprise_id()) && companyCode.equals(param.getEnterprise_id())){
				//说明改lov对应的参数 企业已经自定义过
				id = param.getId();
			}else{
				id = param.getId();
			}
			queryBean.setResultList(portalBaseLovDao.pageListLovByCompany(queryBean.getParamKey(),id));
			//queryBean.setTotalRows(portalBaseLovDao.countListLov(queryBean.getParamKey(),id));
		}
		return queryBean;
	}
	
	@Override
	public PortalBaseLovQueryBean queryLovByUserParameterId(PortalBaseLovQueryBean queryBean) {
		String companyCode = SpringWebUtils.getRequestCompany();
		String username = SpringWebUtils.getRequestUser();
		PortalBaseParameter param = parameterService.getParameter(queryBean.getParamKey(), queryBean.getModuleName(),companyCode,username);
		if(null!=param){
			String id = "";
			if(StringUtils.isNotBlank(param.getEnterprise_id()) && StringUtils.isNotBlank(param.getUser_id()) && username.equals(param.getUser_id()) && companyCode.equals(param.getEnterprise_id())){
				//说明改lov对应的参数 用户已经自定义过
				id = param.getId();
			}else{
				id = companyCode+"."+username+"."+param.getModule_name()+"."+param.getParam_key();
			}
			PageHelper.startPage(queryBean.getStart(), queryBean.getLength());
			queryBean.setResultList(portalBaseLovDao.pageListLov(queryBean.getParamKey(),id));
			queryBean.setTotalRows(portalBaseLovDao.countListLov(queryBean.getParamKey(),id));
		}
		return queryBean;
	}

	

	@Override
	public void updateCompanyLov(CompanyBaseLovBean lov) {
		String companyCode = StringUtils.isBlank(lov.getCompanyCode())?SpringWebUtils.getRequestCompany():lov.getCompanyCode();
		
		PortalBaseParameter param = parameterService.getParameter(lov.getParam_key(), lov.getModule_name(),companyCode,null);
		if(null!=param){
			if(!(StringUtils.isNotBlank(param.getEnterprise_id()) && companyCode.equals(param.getEnterprise_id()))){
				//参数没有被企业自定义过
				PortalBaseParameter p = new PortalBaseParameter();
				p.setId(companyCode+"."+param.getId());
				p.setEnterprise_id(companyCode);
				p.setModule_name(param.getModule_name());
				p.setParam_key(param.getParam_key());
				p.setI18n_param_key(param.getI18n_param_key());
				p.setParam_desc(param.getParam_desc());
				p.setParam_level(CommonConstant.ParameterLevel.ENTERPRISE);
				p.setData_type(param.getData_type());
				p.setParam_value(param.getParam_value());
				p.setValid(1L);
				p.setCompany_visible(1L);
				p.setUser_visible(0L);
				jdbcDao.insert(p);
				//查询出平台定义的LOV 赋给企业
				List<PortalBaseLov> lovList = portalBaseLovDao.queryListByParamKey(param.getParam_key());
				for(PortalBaseLov pl : lovList){
					pl.setId(companyCode+"."+param.getModule_name()+"."+param.getParam_key()+"."+pl.getOption_key());
					pl.setEnterprise_id(companyCode);
					pl.setCreate_time(new Date());
					if(pl.getPortal_base_lov_id().intValue()==lov.getPortal_base_lov_id().intValue()){
						pl.setOption_desc(lov.getOption_desc());
						pl.setValid(lov.getValid());
						pl.setOption_order(lov.getOption_order());
					}
					pl.setPortal_base_lov_id(null);
					jdbcDao.insert(pl);
				}
			}else{
				jdbcDao.update(Criteria.create(PortalBaseLov.class).set("option_desc", lov.getOption_desc()).set("valid", lov.getValid()).set("option_order", lov.getOption_order()).where("portal_base_lov_id", new Object[]{lov.getPortal_base_lov_id()}).and("enterprise_id", new Object[]{companyCode}));
			}
		}
		
		
		stringRedisTemplate.opsForHash().put(CommonConstant.SESSION_LOV_VERSION, "E_"+companyCode, UUID.randomUUID().toString());
		
		
		
		
		
	}

	@Override
	public void delCompanyLov(DelCompanyLovBean bean) {
		String companyCode = StringUtils.isBlank(bean.getCompanyCode())?SpringWebUtils.getRequestCompany():bean.getCompanyCode();
		PortalBaseParameter param = parameterService.getParameter(bean.getParamKey(), bean.getModuleName(),companyCode,null);
		if(null!=param){
			if(!(StringUtils.isNotBlank(param.getEnterprise_id()) && companyCode.equals(param.getEnterprise_id()))){
				//参数没有被企业自定义过
				PortalBaseParameter p = new PortalBaseParameter();
				p.setId(companyCode+"."+param.getId());
				p.setEnterprise_id(companyCode);
				p.setModule_name(param.getModule_name());
				p.setParam_key(param.getParam_key());
				p.setI18n_param_key(param.getI18n_param_key());
				p.setParam_desc(param.getParam_desc());
				p.setParam_level(CommonConstant.ParameterLevel.ENTERPRISE);
				p.setData_type(param.getData_type());
				p.setParam_value(param.getParam_value());
				p.setValid(1L);
				p.setCompany_visible(1L);
				p.setUser_visible(0L);
				jdbcDao.insert(p);
				//查询出平台定义的LOV 赋给企业
				List<PortalBaseLov> lovList = portalBaseLovDao.queryListByParamKey(param.getParam_key());
				for(PortalBaseLov pl : lovList){
					if(!checkIdexist(pl.getPortal_base_lov_id(),bean.getIds())){
						pl.setId(companyCode+"."+param.getModule_name()+"."+param.getParam_key()+"."+pl.getOption_key());
						pl.setEnterprise_id(companyCode);
						pl.setPortal_base_lov_id(null);
						pl.setCreate_time(new Date());
						jdbcDao.insert(pl);
					}
					
				}
			}else{
				jdbcDao.delete(Criteria.create(PortalBaseLov.class).where("portal_base_lov_id",Criteria.FIELD_OPERATOR_IN,bean.getIds()).and("enterprise_id", new Object[]{companyCode}));
			}
		}
		stringRedisTemplate.opsForHash().put(CommonConstant.SESSION_LOV_VERSION, "E_"+companyCode, UUID.randomUUID().toString());
		
	}
	
	private boolean checkIdexist(Long id,Long[] ids){
		for(Long i : ids){
			if(id.intValue()==i.intValue()){
				return true;
			}
		}
		return false;
	}
	
	
	@Override
	public void addCompanyLov(CompanyBaseLovBean lov) {
		String companyCode = StringUtils.isBlank(lov.getCompanyCode())?SpringWebUtils.getRequestCompany():lov.getCompanyCode();
		PortalBaseParameter param = parameterService.getParameter(lov.getParam_key(), lov.getModule_name(),companyCode,null);
		if(null!=param){
			if(!(StringUtils.isNotBlank(param.getEnterprise_id()) && companyCode.equals(param.getEnterprise_id()))){
				//参数没有被企业自定义过
				PortalBaseParameter p = new PortalBaseParameter();
				p.setId(companyCode+"."+param.getId());
				p.setEnterprise_id(companyCode);
				p.setModule_name(param.getModule_name());
				p.setParam_key(param.getParam_key());
				p.setI18n_param_key(param.getI18n_param_key());
				p.setParam_desc(param.getParam_desc());
				p.setParam_level(CommonConstant.ParameterLevel.ENTERPRISE);
				p.setData_type(param.getData_type());
				p.setParam_value(param.getParam_value());
				p.setValid(1L);
				p.setCompany_visible(1L);
				p.setUser_visible(0L);
				jdbcDao.insert(p);
				//查询出平台定义的LOV 赋给企业
				List<PortalBaseLov> lovList = portalBaseLovDao.queryListByParamKey(param.getParam_key());
				for(PortalBaseLov pl : lovList){
					pl.setId(companyCode+"."+param.getModule_name()+"."+param.getParam_key()+"."+pl.getOption_key());
					pl.setEnterprise_id(companyCode);
					pl.setPortal_base_lov_id(null);
					//pl.setCreate_time(new Date());
					jdbcDao.insert(pl);
					
				}
			}
			PortalBaseLov olov = new PortalBaseLov();
			String opk = lov.getOption_key()!=null ? lov.getOption_key():UUID.randomUUID().toString();
			olov.setId(companyCode+"."+param.getModule_name()+"."+param.getParam_key()+"."+opk);
			olov.setEnterprise_id(companyCode);
			olov.setModule_name(param.getModule_name());
			olov.setParam_key(param.getParam_key());
			olov.setParent_id(lov.getParent_id()==null?0L:lov.getParent_id());
			olov.setData_type(StringUtils.isNotBlank(lov.getData_type())?lov.getData_type():"String");
			olov.setOption_key(opk);
			olov.setOption_order(lov.getOption_order());
			olov.setOption_desc(lov.getOption_desc());
			jdbcDao.insert(olov);
			
		}
		stringRedisTemplate.opsForHash().put(CommonConstant.SESSION_LOV_VERSION, "E_"+companyCode, UUID.randomUUID().toString());
	}
	
	
	
	@Override
	public void addUserLov(PortalBaseLov lov) {
		
		String companyCode = SpringWebUtils.getRequestCompany();
		String username = SpringWebUtils.getRequestUser();
		
		PortalBaseParameter param = parameterService.getParameter(lov.getParam_key(), lov.getModule_name(),companyCode,username);
		
		if(null!=param){
			if(!(StringUtils.isNotBlank(param.getEnterprise_id()) &&StringUtils.isNotBlank(param.getUser_id()) && companyCode.equals(param.getEnterprise_id())&& username.equals(param.getUser_id()))){
				//参数没有被用户自定义过
				PortalBaseParameter p = new PortalBaseParameter();
				p.setId(companyCode+"."+username+"."+param.getModule_name()+"."+param.getParam_key());
				p.setEnterprise_id(companyCode);
				p.setUser_id(username);
				p.setModule_name(param.getModule_name());
				p.setParam_key(param.getParam_key());
				p.setI18n_param_key(param.getI18n_param_key());
				p.setParam_desc(param.getParam_desc());
				p.setParam_level(CommonConstant.ParameterLevel.USER);
				p.setData_type(param.getData_type());
				p.setParam_value(param.getParam_value());
				p.setValid(1L);
				p.setCompany_visible(1L);
				p.setUser_visible(1L);
				jdbcDao.insert(p);
			}
			if(jdbcDao.querySingleResult(Criteria.create(PortalBaseLov.class).where("id", new Object[]{companyCode+"."+username+"."+param.getModule_name()+"."+param.getParam_key()+"."+lov.getOption_key()}))!=null){
				throw new ServiceException("PARAMLOVEXITS");
			}else{
				lov.setId(companyCode+"."+username+"."+param.getModule_name()+"."+param.getParam_key()+"."+lov.getOption_key());
				lov.setEnterprise_id(companyCode);
				lov.setUser_id(username);
				lov.setModule_name(param.getModule_name());
				lov.setParam_key(param.getParam_key());
				lov.setParent_id(0L);
				jdbcDao.insert(lov);
			}
			stringRedisTemplate.opsForValue().set(CommonConstant.SESSION_LOV_VERSION+"_"+companyCode+username, UUID.randomUUID().toString());
		}else{
			throw new ServiceException("PARAMNOTFOUND");
		}
		stringRedisTemplate.opsForHash().put(CommonConstant.SESSION_LOV_VERSION, "U_"+companyCode+username, UUID.randomUUID().toString());
		
	}

	@Override
	public void updateUserLov(PortalBaseLov lov) {
		jdbcDao.update(Criteria.create(PortalBaseLov.class).set("option_desc", lov.getOption_desc()).set("option_order", lov.getOption_order()).set("valid", lov.getValid()).where("portal_base_lov_id", new Object[]{lov.getPortal_base_lov_id()}).and("enterprise_id", new Object[]{SpringWebUtils.getRequestCompany()}).and("user_id", new Object[]{SpringWebUtils.getRequestUser()}));
		String companyCode = SpringWebUtils.getRequestCompany();
		String username = SpringWebUtils.getRequestUser();
		stringRedisTemplate.opsForHash().put(CommonConstant.SESSION_LOV_VERSION, "U_"+companyCode+username, UUID.randomUUID().toString());
	}

	@Override
	public void delUserLov(Integer id) {
		jdbcDao.delete(Criteria.create(PortalBaseLov.class).where("portal_base_lov_id",new Object[]{id}).and("enterprise_id", new Object[]{SpringWebUtils.getRequestCompany()}).and("user_id", new Object[]{SpringWebUtils.getRequestUser()}));
		String companyCode = SpringWebUtils.getRequestCompany();
		String username = SpringWebUtils.getRequestUser();
		stringRedisTemplate.opsForHash().put(CommonConstant.SESSION_LOV_VERSION, "U_"+companyCode+username, UUID.randomUUID().toString());
	}

	@Override
	public List<PortalBaseLov> queryListByParamKey(String paramKey) {
		// TODO Auto-generated method stub
		return portalBaseLovDao.queryListByParamKey(paramKey);
	}

	@Override
	public Long insertListMap(List<Map<String, String>> map) {
		// TODO Auto-generated method stub
		return portalBaseLovDao.insertListMap(map);
	}

	@Override
	public void recoveryDefault(CompanyRecoveryDefaultLovBean bean) {
		String companyCode = StringUtils.isNotBlank(bean.getCompanyCode())?bean.getCompanyCode():SpringWebUtils.getRequestUser();
		jdbcDao.delete(Criteria.create(PortalBaseLov.class).where("param_key", new String[]{bean.getParamKey()}).and("enterprise_id", new String[]{companyCode}));
		jdbcDao.delete(Criteria.create(PortalBaseParameter.class).where("param_key", new String[]{bean.getParamKey()}).and("enterprise_id", new String[]{companyCode}));
	}




	@Override
	public List<PortalBaseLov> api_queryListByParamKey1(String paramKey) {
		return queryListByParamKey(paramKey);
	}




	@Override
	public List<PortalBaseLov> api_queryListByParamKey2(@RequestBody List<Map<String, Object>> map) {
		return portalBaseLovDao.queryListByParamKeyByMap(map);
	}




	

	
	   
	

	

}
